package manage.controller;

import manage.dao.UserMapper;
import manage.model.Rates;
import manage.model.Record;
import manage.model.User;
import manage.service.impl.RatesServiceImpl;
import manage.util.LogUtil;
import manage.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class RateController {
    @Autowired
    private RatesServiceImpl ratesService;

    @Autowired
    private UserMapper userMapper;

    private User user;
    //返回Rates的list
    @RequestMapping("/rates")
    @ResponseBody
    public Msg RatesData(HttpServletRequest request, Authentication authentication){
        String page = request.getParameter("currentPage");
        String limit = request.getParameter("pageSize");
        UserDetails userDetails =(UserDetails) authentication.getPrincipal();
        user = userMapper.selectByPrimaryKey(userDetails.getUsername());
        return Msg.success().add("data",ratesService.selectAll(page,limit));
    }

    //添加Rates
    @ResponseBody
    @RequestMapping(value = "/addRates",method = RequestMethod.POST)
    public Msg addRates(Rates rates){

        String s= ratesService.insert(rates);
        return Msg.attendanceAdd(s);
    }

    //修改Rates
    @ResponseBody
    @RequestMapping(value = "/addRates",method = RequestMethod.PUT)
    public Msg updateRates(Rates Rates){
        String s= ratesService.update(Rates);
        return Msg.attendanceAdd(s);
    }
    //删除Rates
    @ResponseBody
    @RequestMapping("/delRates")
    public Msg delRates(@RequestBody Integer id){
        String s = ratesService.del(id);
        return Msg.attendanceAdd(s);
    }

    //根据主键查Rates 和修改配合使用
    @ResponseBody
    @RequestMapping("/ratesSelect")
    public Msg selRates(@RequestBody Integer id){
        Rates rates = ratesService.select(id);
        return Msg.success().add("rates",rates);
    }

}
